//
//  CustomCalloutView.m
//  iSolarCloud
//
//  Created by sungrow on 15/11/6.
//  Copyright © 2015年 sungrow. All rights reserved.
//

#import "CustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#import "CalloutCollectionCell.h"

#define kArrorHeight        0
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
CGFloat const MapAnnotationViewH = 165;

@interface CustomCalloutView ()
/** 插图 */
@property (nonatomic,strong)UICollectionView *collectionView;

@end

static NSString *calloutCellID = @"CalloutCell";

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 20.0f;
        flowLayout.headerReferenceSize = CGSizeMake(10, 10);
        flowLayout.footerReferenceSize = CGSizeMake(10, 10);
        self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
        self.collectionView.pagingEnabled = YES;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CalloutCollectionCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:calloutCellID];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.frame = CGRectMake(0, 10, SCREEN_WIDTH, MapAnnotationViewH);
        self.collectionView.frame = self.bounds;
        self.collectionView.backgroundColor = [UIColor clearColor];
        [self.collectionView setNeedsDisplay];
        
        [self addSubview:self.collectionView];
    }
    return self;
}

- (void)setMapPsArr:(NSArray *)mapPsArr {
    _mapPsArr = [NSArray arrayWithArray:mapPsArr];
    
    [self.collectionView reloadData];
    
}
- (void)dismissCalloutView
{
    self.mapPsArr = nil;
    [self removeFromSuperview];
}


#pragma mark - draw rect

- (void)drawRect:(CGRect)rect
{
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
//    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:41/255.0 green:76/255.0 blue:132/255.0 alpha:0.8].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mapPsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CalloutCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:calloutCellID forIndexPath:indexPath];
    cell.layer.cornerRadius = 5.0f;
    cell.index = indexPath.row;
    cell.totalCount = self.mapPsArr.count;
    cell.mapPsModel = self.mapPsArr[indexPath.row];
    
    [cell.tapBtn addTarget:self action:@selector(detailBtnTap:) forControlEvents:UIControlEventTouchUpInside];
    cell.tapBtn.tag = indexPath.row;
    
    return cell;
}

#pragma mark - TapGesture

- (void)detailBtnTap:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(didDetailButtonTapped:)])
    {
        [self.delegate didDetailButtonTapped:button.tag];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH - 20, self.collectionView.frame.size.height);
}

@end
